<?php
	class Mcommodities extends Model {
	
		function __construct() {
			parent::Model();
		}
		
		function getAllCommodities() {
			$query = $this->db->get('commodity');
			if ($query->num_rows() > 0){
				return $query->result(); 
			} else {
				show_error('Database is Empty!');
			}  
		}
		
		function getCommodity($id){
			$options = array('id' => $id);
			$query = $this->db->getwhere('commodity',$options,1);
			if ($query->num_rows() > 0){
				return $query->result(); 
			}   
		}
		
		function addCommodity(){
			$data = array( 
				'name' => $_POST['name'],
				'base_price' => $_POST['base_price']
			);
			$this->db->insert('commodity', $data);	
		}
		
		function updateCommodity ($id){
			$data = array( 
				'name' => $_POST['name'],
				'base_price' => $_POST['base_price']
				);
			date_default_timezone_set('Europe/Helsinki');
			$data['update_at'] = date ("Y-m-d H:i:s");
			$this->db->where('id', $id);
			$this->db->update('commodity', $data);	
		}
		
		function deleteCommodities($id){
			$this->db->where('id', $id);
			$this->db->delete('commodity');
		}
	}
?>